package com.huilang.singleton;

import java.io.*;

public class SIngletonImp {
    public static void main(String[] args) throws Exception{
//        te1();
        te2();
        te2();
    }

    public static void te1() throws IOException {
        Singleton instance = Singleton.getInstance();
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\a.txt"));
        oos.writeObject(instance);
        oos.close();

    }

    public static void te2() throws Exception{
        Singleton instance = Singleton.getInstance();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\a.txt"));
        Singleton singleton= (Singleton) ois.readObject();
        System.out.println(singleton);
    }
}
class Singleton implements Serializable {
    private  static  boolean flag=true;
    private Singleton() {
        if (false) {
            flag=false;
        }
    }

    private static class SingletonNe {
        private static final Singleton singleton=new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonNe.singleton;
    }

    public Object readResolve() {
        return SingletonNe.singleton;
    }

}